package self.hoy.demo.grainmall.coupon.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;
import self.hoy.demo.common.to.MemberPriceTo;
import self.hoy.demo.common.to.SkuReductionTo;
import self.hoy.demo.common.utils.PageUtils;
import self.hoy.demo.common.utils.Query;

import self.hoy.demo.grainmall.coupon.entity.MemberPrice;
import self.hoy.demo.grainmall.coupon.entity.SkuLadder;
import self.hoy.demo.grainmall.coupon.mapper.SkuFullReductionMapper;
import self.hoy.demo.grainmall.coupon.entity.SkuFullReduction;
import self.hoy.demo.grainmall.coupon.service.MemberPriceService;
import self.hoy.demo.grainmall.coupon.service.SkuFullReductionService;
import self.hoy.demo.grainmall.coupon.service.SkuLadderService;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionMapper, SkuFullReduction> implements SkuFullReductionService {

    @Autowired
    private SkuLadderService skuLadderService;

    @Autowired
    private MemberPriceService memberPriceService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReduction> page = this.page(
                new Query<SkuFullReduction>().getPage(params),
                new QueryWrapper<SkuFullReduction>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveSkuReduction(SkuReductionTo to) {
        if (to.getFullCount() > 0) {
            SkuLadder skuLadder = new SkuLadder();
            skuLadder.setSkuId(to.getSkuId());
            skuLadder.setFullCount(to.getFullCount());
            skuLadder.setDiscount(to.getDiscount());
            skuLadder.setAddOther(to.getCountStatus());
            //        skuLadder.setPrice(to.getP);
            skuLadderService.save(skuLadder);
        }

        if (to.getReducePrice().compareTo(BigDecimal.ZERO) > 0) {
            SkuFullReduction reduction = new SkuFullReduction();
            BeanUtils.copyProperties(to, reduction);
            this.save(reduction);
        }

        List<MemberPriceTo> memberPrice = to.getMemberPrice();
        List<MemberPrice> collect = memberPrice.stream().filter(item->{
            return item.getPrice().compareTo(BigDecimal.ZERO) > 0;
        }).map(item -> {
            MemberPrice price = new MemberPrice();
            price.setSkuId(to.getSkuId());
            price.setMemberLevelId(item.getId());
            price.setMemberLevelName(item.getName());
            price.setMemberPrice(item.getPrice());
            price.setAddOther(1);
            return price;
        }).collect(Collectors.toList());
        if (collect.size() > 0) {
            memberPriceService.saveBatch(collect);
        }
    }

}